﻿using System;
using System.Collections.Generic;
using System.Text;

using Daco.Web.Store;
using Daco.Web.UI.WebControls;

namespace Daco.Web.Masters
{
    public class STOREMaster : DACOMaster
    {
        private CategoryTree<int, CProductCategory> _categoryTree = null;
        public CategoryTree<int, CProductCategory> CategoryTree
        {
            get { return _categoryTree; }
        }

        private ULManufacture _manufactureList = null;
        public ULManufacture ManufactureList
        {
            get { return _manufactureList; }
        }

        public STOREMaster()
        {
            this.Load += new EventHandler(STOREMaster_Load);
        }

        protected void STOREMaster_Load(object sender, EventArgs e)
        {
            // get the current page...
            string pageSrc = GetPageSource();

            try
            {
                #region "Bind STORE Template"
                _categoryTree = new UI.WebControls.CategoryTree<int, CProductCategory>();
                _categoryTree.Title = "Danh mục";
                _categoryTree.DataSource = (IEnumerable<ICategory<int, CProductCategory>>)Daco.Web.Store.CProductCategory.GetCategoryTree(-1);
                _categoryTree.DataBind();

                _manufactureList = new ULManufacture();
                _manufactureList.Title = "Hãng sản xuất";
                _manufactureList.DataSource = Daco.Web.Store.CProduct.GetManufactures();
                _manufactureList.DataBind();
                #endregion

                Daco.Web.UI.SitePage currentPage = (Daco.Web.UI.SitePage)Page.LoadControl(pageSrc);

                CurrentPage = currentPage;
                CurrentPage.Master = this;
                CurrentPage.Header = Header;
                CurrentPage.Footer = Footer;
                CurrentPage.Navigator = Navigator;

                Body.Left.Controls.Add(_categoryTree);
                Body.Left.Controls.Add(_manufactureList);
                Body.Right.Controls.Add(CurrentPage);

                CPageContext.Current.CurrentPage = CurrentPage;

                // add the header control before the page rendering...
                Controls.AddAt(0, Header);
                Controls.AddAt(1, Navigator);
                // add the page before the page rendering...
                Controls.Add(Body);
                // add the footer control after the page...
                Controls.Add(Footer);
            }
            catch (System.IO.FileNotFoundException)
            {
                // Phat sinh application exception, thong bao tai nguyen truy xuat khong ton tai hoac khong co tham quyen
                throw new ApplicationException(Daco.Web.Resources.DACO.ResourceNotFound);
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.WriteLine(
                @"<div>
                    <div style=""margin: 0 auto; width: 1000px;"">");

            base.Render(writer);

            writer.WriteLine(
                @"
                    </div>
                </div>
            ");
        }

        protected override string GetPageSource()
        {
            return base.GetPageSource();
        }
    }
}
